using System;
using System.Diagnostics;

using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ProjectPigs.Assets;
using ProjectPigs.Util;
using ProjectPigs.Input;

using ProjectPigs.Scenes;

namespace ProjectPigs
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Process proc;
        RasterizerState raster = new RasterizerState();
        DepthStencilState dStencil = new DepthStencilState();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            raster = RasterizerState.CullCounterClockwise;

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {

            Console.WriteLine("-------------------------------------------------------------");
            Console.WriteLine("- Initializning ProjectPigs");
            // TODO: Add your initialization logic here
            spriteBatch = new SpriteBatch(GraphicsDevice);
            proc = Process.GetCurrentProcess();


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            Console.WriteLine("-------------------------------------------------------------");
            Console.WriteLine("- Loading Content");

            Renderer.Initialize(Content);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Loading Content from the XNA project
            AssetManager.Load(Content);

            List<Map> maps = new List<Map>();

            //Import Map Data
            maps.Add(AssetImporter.importMap("testmap", null));

            //Add all maps to the SceneManager
            SceneManager.Initialize(maps.ToArray());


            SystemInfo.normalFont = Content.Load<SpriteFont>("normalFont");
            SystemInfo.screenSize = new Size(graphics.GraphicsDevice.DisplayMode.Width, graphics.GraphicsDevice.DisplayMode.Height);
            SystemInfo.windowSize = new Size(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            SystemInfo.graphicsDevice = graphics.GraphicsDevice;
            SystemInfo.black = Content.Load<Texture2D>("black");
            SystemInfo.sb = spriteBatch;
            // TODO: use this.Content to load your game content here

            Console.WriteLine("-------------------------------------------------------------");
            Console.WriteLine("- Running ProjectPigs");

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();




            SystemInput.update(Keyboard.GetState());
            SceneManager.update(gameTime);
            // TODO: Add your update logic here
            Debug.setEntry("TotalGameTime: "+gameTime.TotalGameTime, 0, 0);
            Debug.setEntry("WorkingSet: " + System.Environment.WorkingSet+"  ["+((float)System.Environment.WorkingSet / (float)proc.PrivateMemorySize64) * 100f+" %]", 0, 1);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {

            GraphicsDevice.Clear(Color.Black);
            SceneManager.draw(spriteBatch);
            spriteBatch.Begin();

            Debug.draw(spriteBatch);

            spriteBatch.End();

            resetRenderStates();
            // TODO: Add your drawing code here

            base.Draw(gameTime);

            FPSCounter.drawFrame(spriteBatch, gameTime);
        }

        private void resetRenderStates()
        {
            GraphicsDevice device = SystemInfo.graphicsDevice;
            device.BlendState = BlendState.Opaque;
            device.DepthStencilState = DepthStencilState.Default;
            

        }

    }
}
